package com.zds.a_controller;

import com.zds.y_entity.ResultEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @className:CommonController
 * @date: 2025/1/21 上午11:13
 * @description:
 */
@Component
@RequestMapping
public class CommonController {
    //    -------------------------
    @GetMapping("/q")
    @ResponseBody
    public String getMsg(@RequestParam(value = "message") String msg) {
        System.out.println("获取的消息:" + msg);
        return msg;
    }

    /**
     * 日期转换器例子
     *
     * @param date
     * @return
     */
    @ResponseBody
    @GetMapping({"/date", "/time"})
    public ResultEntity getDate(@RequestParam("date") Date date) {
        System.out.println(date);
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(date);
        return resultEntity;
    }

    @GetMapping("/api")
    public ResultEntity getApi(HttpServletRequest request,
                               HttpServletResponse response,
                               HttpServlet servlet,
                               HttpSession session) {
        ResultEntity resultEntity = new ResultEntity();
        return resultEntity;
    }

    /**
     * 获得请求参数信息
     *
     * @param userAgent
     * @return
     */
    @RequestMapping("/request")
    public ResultEntity getRequest(@RequestHeader(value = "User-Agent") String userAgent,
                                   @RequestHeader("cookie") String cookie
    ) {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(userAgent);
        resultEntity.setMsg(cookie);
        return resultEntity;
    }

    @GetMapping("/upload")
    public String showUploadForm() {
        return "upload";
    }

    @PostMapping("/upload")
    public ResultEntity upload(@RequestParam("username") String name,
                               @RequestParam("upload") MultipartFile file, RedirectAttributes redirectAttributes) {
        ResultEntity resultEntity = new ResultEntity();
        String originalFilename = file.getOriginalFilename();
        try {
            file.transferTo(new File("E:\\ssm\\" + originalFilename));
            resultEntity.setMsg(name + "上传了文件" + originalFilename);
            resultEntity.setCode(200);
            redirectAttributes.addFlashAttribute("message", "文件上传成功");
        } catch (IOException e) {
            resultEntity.setMsg(name + "上传了文件" + originalFilename);
            resultEntity.setCode(500);
            redirectAttributes.addFlashAttribute("message", "文件上传失败:"+e.getMessage());
        }

        return resultEntity;
    }

    @GetMapping("/exception")
    @ResponseBody
    public ModelAndView getException() {
        int i = 1 / 0;
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }


}
